﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.HU;
using HiStaff.Util;
using HiStaff.Dal;
using System.Windows.Forms;

namespace HiStaff.iProfile
{
    public partial class EmployeeProfile : FormBase
    {
        public EmployeeProfile()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            try
            {
                AddBasicCommandBar();
                base.FormLoad();
                LoadCombo();
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected void LoadCombo()
        {
            try
            {
                Common.BindOrgSignContractToCombobox(cboContractOrg);
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = chkWorkingType.Value });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void BindDataFromGrid()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    ValueObjectToControls(row.GetRow().DataRow, this);
                }
            }
        }

        private void chkWorkingType_CheckedChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void trvOrg_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            BindData();
        }

        private void hiGrid1_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGridToForm(grdMain, this);
            //QUAN LY TRUC TIEP
            this.Cursor = Cursors.WaitCursor;
            if (grdMain.SelectedItems.Count > 0)
            {
                if (grdMain.SelectedItems[0].RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    var obj = new HU_LINEMANAGER();
                    obj.WORKINGID = ((HU_EMPLOYEE)(grdMain.SelectedItems[0].GetRow().DataRow)).ID;
                    var lstLineMgt = OracleHelper.ExcuteSelectMultiObject<EMPLOYEE_SELECTION>("APRS_HU_LINEMANAGER", obj);
                    cboLineMng.EmployeeSelected = lstLineMgt;
                }
                else cboLineMng.EmployeeSelected = null;
            }
            else
            {
                cboLineMng.EmployeeSelected = null;
            }
            this.Cursor = Cursors.Default;
        }

        protected bool CheckData()
        {
            //Thong tin quyet dinh
            if (!Common.CheckEffectDateAndExpireDate(txtWEffectDate, txtWExpireDate))
            {
                MessageBox.Show(string.Format(GetMessageById("MSG_INVALID_DATE_AUTO"), "Thông tin công tác: ngày kết thúc", "ngày hiệu lực"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            //Thong tin hợp đồng
            if (!Common.CheckEffectDateAndExpireDate(txtCEffectDate, txtExpireDate))
            {
                MessageBox.Show(string.Format(GetMessageById("MSG_INVALID_DATE_AUTO"), "Thông tin hợp đồng: ngày kết thúc", "ngày bắt đầu"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            //Thong tin thu nhập
            if (!Common.CheckEffectDateAndExpireDate(txtSExpireDate, txtSEffectDate))
            {
                MessageBox.Show(string.Format(GetMessageById("MSG_INVALID_DATE_AUTO"), "Thông tin thu nhập: ngày kết thúc", "ngày hiệu lực"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            //Thong tin nhan vien
            if (!Common.CheckEffectDateAndExpireDate(txtPassExpire, txPassEffect))
            {
                MessageBox.Show(string.Format(GetMessageById("MSG_INVALID_DATE_AUTO"), "Người nước ngoài: ngày hết hạn", "ngày ngày cấp"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            string sCODE = txtCode.Text;
            string sIDNO = txtID.Text;
            if (!Common.CheckIDNo(sCODE, sIDNO))
            {
                MessageBox.Show(GetMessageById("MSG_INVALID_IDNO"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
            return true;
        }

        protected override bool SaveButtonClick()
        {
            if (!CheckData())
            {
                return false;
            }
            var objSave = new HU_EMPLOYEE();
            ValueControlsToObject(objSave, this);
            if (OracleHelper.ExcuteNonQuery("PRU_EMPLOYEE", objSave))
            {
                BindData();
                return true;
            }
            else return false;
        }

        protected override bool CancelButtonClick()
        {
            BindDataFromGrid();
            return true;
        }

        private void cboContractType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ResetExpireDate();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void ResetExpireDate()
        {
            if (BaseStatus != Util.Enum.FormStatus.View)
            {
                int m = Convert.ToInt16(((System.Data.DataRowView)(cboContractType.SelectedItem.DataRow)).Row.ItemArray[3]);
                if (m == 0 || txtCEffectDate.IsNullDate == true)
                {
                    txtExpireDate.IsNullDate = true;
                }
                else
                {
                    txtExpireDate.IsNullDate = false;
                    txtExpireDate.Value = (DateTime)(txtCEffectDate.Value).Value.AddMonths(m);
                }
            }
        }

        private void txtSalary_ValueChanged(object sender, EventArgs e)
        {
            TotalCost();
        }

        private void txtPercentSal_ValueChanged(object sender, EventArgs e)
        {
            TotalCost();
        }

        private void TotalCost()
        {
            try
            {
                txtTotalSal.Value = txtSalary.Value * txtPercentSal.Value / 100;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void cboPC_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cboPC.SelectedValue == null)
                {
                    txtMin.Value = null;
                    txtMax.Value = null;
                }
                else
                {
                    txtMin.Value = ConvertHelper.ToDecimal(((System.Data.DataRowView)(cboPC.SelectedItem.DataRow)).Row.ItemArray[3]);
                    txtMax.Value = ConvertHelper.ToDecimal(((System.Data.DataRowView)(cboPC.SelectedItem.DataRow)).Row.ItemArray[4]);
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void txtSalary_Leave(object sender, EventArgs e)
        {
            try
            {
                if (txtSalary.Value > 0 && (txtMin.Value > txtSalary.Value || txtSalary.Value > txtMax.Value))
                {
                    if (MessageBox.Show("Mức lương không nằm trong khoảng Min-Max. Tiếp tục?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.Cancel)
                    {
                        txtSalary.Value = 0;
                    }
                    else
                    {
                        TotalCost();
                    }
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
    }
}
